package com.ang.jackson;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ssang
 * @date 2023-07-26 14:15
 */
public class Test {
    public static void main(String[] args) throws Exception{
        Test t = new Test();
        t.test();
    }

    //-------------------------------对象-------------------------------
    public void test1() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        User1 user1 = new User1(1,"test",20);

        //转为string
        String str = objectMapper.writeValueAsString(user1);
        System.out.println(str);

        //直接使用class
        User2 user2 = objectMapper.readValue(str, User2.class);
        System.out.println(user2.toString());
    }

    public void test2() throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        User1 user1 = new User1(1,"test",20);

        //转为string
        String str = objectMapper.writeValueAsString(user1);
        System.out.println(str);

        //使用javatype，一般User2.class是作为参入传进来的
        JavaType javaType = objectMapper.getTypeFactory().constructType(User2.class);

        User2 user2 = objectMapper.readValue(str, javaType);
        System.out.println(user2.toString());
    }

    //-------------------------------List-------------------------------
    public void test3() throws JsonProcessingException{
        ObjectMapper objectMapper = new ObjectMapper();
        List<User1> list1 = new ArrayList<>();
        User1 user1 = new User1(1,"test",20);
        list1.add(user1);

        String str = objectMapper.writeValueAsString(list1);
        System.out.println(str);

        //这里只是转成了List类型，里面的对象没有变成User2
        List<User2> list2 = objectMapper.readValue(str, List.class);
        System.out.println(JSON.toJSONString(list2));
        //上面的list2，相当于下面的list
        List list = objectMapper.readValue(str, List.class);
        System.out.println(JSON.toJSONString(list));
    }

    public void test4() throws JsonProcessingException{
        ObjectMapper objectMapper = new ObjectMapper();
        List<User1> list1 = new ArrayList<>();
        User1 user1 = new User1(1,"test",20);
        list1.add(user1);

        String str = objectMapper.writeValueAsString(list1);
        System.out.println(str);

        //使用javatype，一般User2.class是作为参入传进来的
        JavaType javaType = objectMapper.getTypeFactory().constructCollectionType(List.class,User2.class);

        //list2中的元素，是User2类型
        List<User2> list2 = objectMapper.readValue(str, javaType);
        System.out.println(JSON.toJSONString(list2));
    }

    //-------------------------------Map-------------------------------
    public void test5() throws JsonProcessingException{
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String,User1> map1 = new HashMap<>();
        User1 user1 = new User1(1,"test",20);
        map1.put("t", user1);

        String str = objectMapper.writeValueAsString(map1);
        System.out.println(str);

        //直接转Map
        Map<String,User2> map2 = objectMapper.readValue(str, Map.class);
        System.out.println(JSON.toJSONString(map2));
    }

    public void test6() throws JsonProcessingException{
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String,User1> map1 = new HashMap<>();
        User1 user1 = new User1(1,"test",20);
        map1.put("t", user1);

        String str = objectMapper.writeValueAsString(map1);
        System.out.println(str);

        //使用javaType，一般User2.class是作为参入传进来的
        JavaType javaType = objectMapper.getTypeFactory().constructMapType(Map.class, String.class, User2.class);

        Map<String,User2> map2 = objectMapper.readValue(str, javaType);
        System.out.println(JSON.toJSONString(map2));
    }

    //-------------------------------复杂类型：R<Map<String,List<User2>>>-------------------------------
    public void test() throws JsonProcessingException{
        ObjectMapper objectMapper = new ObjectMapper();
        //list
        List<User1> list1 = new ArrayList<>();
        User1 user1 = new User1(1,"test",20);
        list1.add(user1);
        //map
        Map<String, List<User1>> map1 = new HashMap<>();
        map1.put("t", list1);
        //R
        R r = new R("200", map1);

        String str = objectMapper.writeValueAsString(r);
        System.out.println(str);

        //使用javatype，由里到外，一层一层的创建
        //list
        JavaType javaTypeList = objectMapper.getTypeFactory().constructCollectionType(List.class, User2.class);
        System.out.println(javaTypeList);
        //string
        JavaType javaTypeString = objectMapper.getTypeFactory().constructType(String.class);
        System.out.println(javaTypeString);
        //map
        JavaType javaTypeMap = objectMapper.getTypeFactory().constructMapType(Map.class, javaTypeString, javaTypeList);
        System.out.println(javaTypeMap);
        //R
        JavaType javaTypeR = objectMapper.getTypeFactory().constructParametricType(R.class, javaTypeMap);
        System.out.println(javaTypeR);

        R<Map<String, List<User2>>> result = objectMapper.readValue(str, javaTypeR);
        System.out.println(JSON.toJSONString(result));

    }


    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    static class User1{
        private Integer id;
        private String name;
        private Integer age;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    static class User2{
        private Integer id;
        private String name;
        private Integer age;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    static class R<T>{
        private String code;
        private T data;
    }
}
